#include <stdlib.h>
#include "deviser.h"

int main(int argc, char** argv) {
	init_gc();
	gc_halt();  //XXX
	ext_ref env;
	env.ref = NULL;
	env.next = root_set;
	root_set = &env;
	FILE* input = stdin;

	if(argc > 1) {
		input = fopen(argv[1], "r");
		if(input == NULL) {
			printf("Cannot open file %s\n", argv[1]);
			exit(1);
		}
	}

	env.ref = mk_default_env();
	
	init_eval();
	if(input == stdin) {
		printf("Environment: "); lisp_print(env.ref, stdout); fprintf(stdout, "\n");
	}
	while(!feof(input)) {
		if(input == stdin) printf("> ");
		gc_halt();  //XXX
		cell r = lisp_read(input);
		gc_resume();
		if(r == NULL) continue;
		cell c = eval(r, env.ref);
		if(input == stdin) {
			lisp_print(c, stdout); fprintf(stdout, "\n");
		}
	}

	printf("num cons: %i\n", num_cons);
	return 0;
}
